package com.oocl.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.oocl.dao.FoodTypeDao;
import com.oocl.factory.DBConFactory;
import com.oocl.log4j.Log4j;
import com.oocl.pojo.FoodType;

public class FoodTypeDaoImpl implements FoodTypeDao {

	@Override
	public int save(FoodType type) {
		String sql = "insert into foodtype values(?,?)";
		Connection con = DBConFactory.getConnection();
		if (con == null) {
			Log4j.getLogger().debug("can not get Connection");
			return 0;
		}
		PreparedStatement stat = null;
		try {
			stat = con.prepareStatement(sql);
			stat.setString(1, type.getId());
			stat.setString(2, type.getName());
			int effectedRow = stat.executeUpdate();
			Log4j.getLogger().debug("insert have done,{} row effected", effectedRow);
			return effectedRow;
		} catch (SQLException e) {
			Log4j.getLogger().error("[ 保存 FoodType 出错 ] {}", e.fillInStackTrace());
		} finally {
			DBConFactory.close(con, stat, null);
		}
		return 0;
	}

	@Override
	public int delete(String id) {
		String sql = "delete from foodtype where id = ?";
		Connection con = DBConFactory.getConnection();
		if (con == null) {
			Log4j.getLogger().debug("can not get Connection");
		}
		PreparedStatement stat = null;
		try {
			stat = con.prepareStatement(sql);
			stat.setString(1, id);
			int effectiveRow = stat.executeUpdate();
			Log4j.getLogger().debug("delete have done,{} row effected", effectiveRow);
			return effectiveRow;
		} catch (SQLException e) {
			Log4j.getLogger().error("[ 根据Id 删除 food 出错 ] {}", e.fillInStackTrace());
		} finally {
			DBConFactory.close(con, stat, null);
		}
		return 0;
	}

	@Override
	public int update(FoodType type) {
		String sql = "update foodtype set type_name=? where id=?";
		Connection con = DBConFactory.getConnection();
		if (con == null) {
			Log4j.getLogger().debug("can not get Connection");
		}
		PreparedStatement stat = null;
		try {
			stat = con.prepareStatement(sql);
			stat.setString(1, type.getName());
			stat.setString(2, type.getId());
			int effectedRow = stat.executeUpdate();
			Log4j.getLogger().debug("update have done,{} row effected", effectedRow);
			return effectedRow;
		} catch (SQLException e) {
			Log4j.getLogger().error("[ 更新 foodType 出错 ] {}", e.fillInStackTrace());
		} finally {
			DBConFactory.close(con, stat, null);
		}

		return 0;
	}

	@Override
	public FoodType find(String id) {
		String sql = "select * from foodtype where id = ?";
		Connection con = DBConFactory.getConnection();
		PreparedStatement stat = null;
		ResultSet rs = null;
		try {
			stat = con.prepareStatement(sql);
			stat.setString(1, id);
			rs = stat.executeQuery();
			if (rs.next()) {
				FoodType type = new FoodType();
				type.setId(rs.getString(1));
				type.setName(rs.getString(2));
				Log4j.getLogger().debug("find have done,{} have been found", type);
				return type;
			}
		} catch (SQLException e) {
			Log4j.getLogger().error("[ 由 id 查找FoodType出错 ] {}", e.fillInStackTrace());
		} finally {
			DBConFactory.close(con, stat, rs);
		}

		return null;
	}

	@Override
	public List<FoodType> findAll() {
		String sql = "select * from foodtype";
		List<FoodType> types = new ArrayList<>();
		Connection con = DBConFactory.getConnection();
		PreparedStatement stat = null;
		ResultSet rs = null;
		try {
			stat = con.prepareStatement(sql);
			rs = stat.executeQuery();
			while (rs.next()) {
				FoodType type = new FoodType();
				type.setId(rs.getString(1));
				type.setName(rs.getString(2));
				Log4j.getLogger().debug("findAll doing ,{} have been find", type);
				types.add(type);
			}
		} catch (SQLException e) {
			Log4j.getLogger().error("[ 查找所有的食品信息出错 ] {}", e.fillInStackTrace());
		} finally {
			DBConFactory.close(con, stat, rs);
		}
		return types;
	}

}
